using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace BTS_Utilities.EDI_Fixup.HLRenumbering
{
    public static class Renumber_837I
    {
        /*
         * We need to renumber the following parent and childitems:
         * (Parent) 1 - [HL_BillingPayToProviderHierarchicalLevel_TS837Q3_2000A] found:
         *   - /*[local-name()='X12EnrichedMessage_X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006/EnrichedMessage_X12_00401_837_I']/*[local-name()='TransactionSet' and namespace-uri()='']/*[local-name()='X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='HL_BillingPayToProviderHierarchicalLevel_TS837Q3_2000A' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='HL01__HierarchicalIDNumber' and namespace-uri()='']
         * (Parent) 2 - [subscriber HL ID - HL01__HierarchicalIDNumber]
         *   - /*[local-name()='X12EnrichedMessage_X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006/EnrichedMessage_X12_00401_837_I']/*[local-name()='TransactionSet' and namespace-uri()='']/*[local-name()='X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000B_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='HL_SubscriberHierarchicalLevel_TS837Q3_2000B' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='HL01__HierarchicalIDNumber' and namespace-uri()='']
         * (Child) 2a - HL02__HierarchicalParentIDNumber
         *   - /*[local-name()='X12EnrichedMessage_X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006/EnrichedMessage_X12_00401_837_I']/*[local-name()='TransactionSet' and namespace-uri()='']/*[local-name()='X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000B_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='HL_SubscriberHierarchicalLevel_TS837Q3_2000B' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='HL02__HierarchicalParentIDNumber' and namespace-uri()='']
         * (Parent) 3 - [patient HL ID - HL_PatientHierarchicalLevel_TS837Q3_2000C]
         *   - /*[local-name()='X12EnrichedMessage_X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006/EnrichedMessage_X12_00401_837_I']/*[local-name()='TransactionSet' and namespace-uri()='']/*[local-name()='X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000B_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000C_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='HL_PatientHierarchicalLevel_TS837Q3_2000C' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='HL01__HierarchicalIDNumber' and namespace-uri()='']
         * (Child) 3a - [patient parent id HL02__HierarchicalParentIDNumber]
         *   - /*[local-name()='X12EnrichedMessage_X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006/EnrichedMessage_X12_00401_837_I']/*[local-name()='TransactionSet' and namespace-uri()='']/*[local-name()='X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000B_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000C_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='HL_PatientHierarchicalLevel_TS837Q3_2000C' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='HL02__HierarchicalParentIDNumber' and namespace-uri()='']
         * 
         * pseudo code should look like this:
         * For  each BillingPRovider
         *      Get Unique ID
         *          For each Subscriber
         *              Get Unique ID
         *              Set parent to BillingProviderHL Level
         *                  For each Patient
         *                      Get UniqueID
         *                      Set Parent to Subscriber level
         */

        internal static string getXPathToBillingHL01(int billingIterator)
        {
            return @"/*[local-name()='X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006'][" + billingIterator + "]/*[local-name()='HL_BillingPayToProviderHierarchicalLevel_TS837Q3_2000A' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='HL01__HierarchicalIDNumber' and namespace-uri()='']";
        }
        internal static string getXPathToSubscriberLoop(int billingIterator)
        {
            return @"/*[local-name()='X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006'][" + billingIterator + "]/*[local-name()='TS837Q3_2000B_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']";
        }

        internal static string getXPathToSubscriberHL01(int subscriberIterator, int billingIterator)
        {
            return @"/*[local-name()='X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006'][" + billingIterator + "]/*[local-name()='TS837Q3_2000B_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006'][" + subscriberIterator + "]/*[local-name()='HL_SubscriberHierarchicalLevel_TS837Q3_2000B' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='HL01__HierarchicalIDNumber' and namespace-uri()='']";
        }
        internal static string getXPathToSubscriberHL02(int subscriberIterator, int billingIterator)
        {
            return @"/*[local-name()='X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006'][" + billingIterator + "]/*[local-name()='TS837Q3_2000B_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006'][" + subscriberIterator + "]/*[local-name()='HL_SubscriberHierarchicalLevel_TS837Q3_2000B' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='HL02__HierarchicalParentIDNumber' and namespace-uri()='']";
        }
        internal static string getXPathToSubscriberHL04(int subscriberIterator, int billingIterator)
        {
            return @"/*[local-name()='X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006'][" + billingIterator + "]/*[local-name()='TS837Q3_2000B_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006'][" + subscriberIterator + "]/*[local-name()='HL_SubscriberHierarchicalLevel_TS837Q3_2000B' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='HL04__HierarchicalChildCode' and namespace-uri()='']";
        }
        internal static string getXPathToPatientLoop(int subscriberIterator, int billingIterator)
        {
            return @"/*[local-name()='X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006'][" + billingIterator + "]/*[local-name()='TS837Q3_2000B_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006'][" + subscriberIterator + "]/*[local-name()='TS837Q3_2000C_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']";
        }
        internal static string getXPathToPatientHL01(int subscriberIterator, int billingIterator, int patientIterator)
        {
            return @"/*[local-name()='X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006'][" + billingIterator + "]/*[local-name()='TS837Q3_2000B_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006'][" + subscriberIterator + "]/*[local-name()='TS837Q3_2000C_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006'][" + patientIterator + "]/*[local-name()='HL_PatientHierarchicalLevel_TS837Q3_2000C' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='HL01__HierarchicalIDNumber' and namespace-uri()='']";
        }
        internal static string getXPathToPatientHL02(int subscriberIterator, int billingIterator, int patientIterator)
        {
            return @"/*[local-name()='X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006'][" + billingIterator + "]/*[local-name()='TS837Q3_2000B_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006'][" + subscriberIterator + "]/*[local-name()='TS837Q3_2000C_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006'][" + patientIterator + "]/*[local-name()='HL_PatientHierarchicalLevel_TS837Q3_2000C' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='HL02__HierarchicalParentIDNumber' and namespace-uri()='']";
        }
        internal static string getXPathToPatientHL04(int subscriberIterator, int billingIterator, int patientIterator)
        {
            return @"/*[local-name()='X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006'][" + billingIterator + "]/*[local-name()='TS837Q3_2000B_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006'][" + subscriberIterator + "]/*[local-name()='TS837Q3_2000C_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006'][" + patientIterator + "]/*[local-name()='HL_PatientHierarchicalLevel_TS837Q3_2000C' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='HL04__HierarchicalChildCode' and namespace-uri()='']";
        }

        public static void Renumber(XmlDocument document)
        {
            int i = 1;
            //2000A Loop
            string XPathToBillingLoop = @"/*[local-name()='X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']";

            int billingIteratorID = 0;
            int subscriberIteratorID = 0;
            int patientIteratorID = 0;

            //Get ParentBilling Loop Nodes
            XmlNodeList _n0 = document.SelectNodes(XPathToBillingLoop);
            //Loop through nodes in 2000A Loop
            for (int billingID = 0; billingID < _n0.Count; billingID++)
            {
                billingIteratorID = billingID + 1;
                //Record the parentID
                int parentBillingID = i;
                //Set the HL Level
                _n0[billingID].SelectSingleNode(getXPathToBillingHL01(billingIteratorID)).InnerText = i.ToString();
                i++;
                //Get the Subscriber Loop Nodes
                billingIteratorID = billingID + 1;
                XmlNodeList _n1 = _n0[billingID].SelectNodes(getXPathToSubscriberLoop(billingIteratorID));
                for (int subID = 0; subID < _n1.Count; subID++)
                {
                    //Record the SubscriberParentID in case there are Patients
                    int subscriberParentID = i;
                    subscriberIteratorID = subID + 1;
                    //Set the HL01
                    _n1[subID].SelectNodes(getXPathToSubscriberHL01(subscriberIteratorID,billingIteratorID))[0].InnerText = i.ToString();
                    //Set the Parent HL02
                    _n1[subID].SelectNodes(getXPathToSubscriberHL02(subscriberIteratorID,billingIteratorID))[0].InnerText = parentBillingID.ToString();
                    i++;
                    //Get the Patient Loop Nodes
                    XmlNodeList _n2 = _n1[subID].SelectNodes(getXPathToPatientLoop(subscriberIteratorID,billingIteratorID));

                    //Set the Child Code for the subscriber.  If there are Patients set it to 1 if not set it to 0
                    //HL04
                    if (_n2.Count > 0)
                    {
                        _n1[subID].SelectNodes(getXPathToSubscriberHL04(subscriberIteratorID,billingIteratorID))[0].InnerText = "1";
                    }
                    else
                    {
                        _n1[subID].SelectNodes(getXPathToSubscriberHL04(subscriberIteratorID,billingIteratorID))[0].InnerText = "0";
                    }

                    for (int patID = 0; patID < _n2.Count; patID++)
                    {
                        int patientParentID = i;
                        patientIteratorID = patID + 1;
                        //Set the HL01
                        _n2[patID].SelectNodes(getXPathToPatientHL01(subscriberIteratorID,billingIteratorID, patientIteratorID))[0].InnerText = i.ToString();
                        //Set the parent HL02
                        _n2[patID].SelectNodes(getXPathToPatientHL02(subscriberIteratorID,billingIteratorID,patientIteratorID))[0].InnerText = subscriberParentID.ToString();
                        i++;
                    }
                }
            }
            
        }

    }
}
